home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 25 / AACD 25.iso / AACD / Magazine / Online / QMail / source / install.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-10  |  3.9 KB  |  170 lines

  1. #include "substdio.h"
  2. #include "stralloc.h"
  3. #include "getln.h"
  4. #include "readwrite.h"
  5. #include "exit.h"
  6. #include "open.h"
  7. #include "error.h"
  8. #include "strerr.h"
  9. #include "byte.h"
  10. #include "fifo.h"
  11.  
  12. stralloc target = {0};
  13. char *to;
  14.  
  15. #define FATAL "install: fatal: "
  16. void nomem() { strerr_die2x(111,FATAL,"out of memory"); }
  17.  
  18. char inbuf[SUBSTDIO_INSIZE];
  19. char outbuf[SUBSTDIO_OUTSIZE];
  20. substdio ssin;
  21. substdio ssout;
  22.  
  23. void doit(line)
  24. stralloc *line;
  25. {
  26.   char *x;
  27.   unsigned int xlen;
  28.   unsigned int i;
  29.   char *type;
  30.   char *uidstr;
  31.   char *gidstr;
  32.   char *modestr;
  33.   char *mid;
  34.   char *name;
  35.   unsigned long uid;
  36.   unsigned long gid;
  37.   unsigned long mode;
  38.   int fdin;
  39.   int fdout;
  40.   unsigned long zlen;
  41.  
  42.   x = line->s; xlen = line->len;
  43.  
  44.   type = x;
  45.   i = byte_chr(x,xlen,':'); if (i == xlen) return;
  46.   x[i++] = 0; x += i; xlen -= i;
  47.  
  48.   uidstr = x;
  49.   i = byte_chr(x,xlen,':'); if (i == xlen) return;
  50.   x[i++] = 0; x += i; xlen -= i;
  51.  
  52.   gidstr = x;
  53.   i = byte_chr(x,xlen,':'); if (i == xlen) return;
  54.   x[i++] = 0; x += i; xlen -= i;
  55.  
  56.   modestr = x;
  57.   i = byte_chr(x,xlen,':'); if (i == xlen) return;
  58.   x[i++] = 0; x += i; xlen -= i;
  59.  
  60.   mid = x;
  61.   i = byte_chr(x,xlen,':'); if (i == xlen) return;
  62.   x[i++] = 0; x += i; xlen -= i;
  63.  
  64.   name = x;
  65.   i = byte_chr(x,xlen,':'); if (i == xlen) return;
  66.   x[i++] = 0; x += i; xlen -= i;
  67.  
  68.   if (!stralloc_copys(&target,to)) nomem();
  69.   if (!stralloc_cats(&target,mid)) nomem();
  70.   if (!stralloc_cats(&target,name)) nomem();
  71.   if (!stralloc_0(&target)) nomem();
  72.  
  73.   uid = -1; if (*uidstr) scan_ulong(uidstr,&uid);
  74.   gid = -1; if (*gidstr) scan_ulong(gidstr,&gid);
  75.   scan_8long(modestr,&mode);
  76.  
  77.   switch(*type) {
  78.     case 'z':
  79.       scan_ulong(type + 1,&zlen);
  80.  
  81.       fdout = open_trunc(target.s);
  82.       if (fdout == -1)
  83.     strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  84.       substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof(outbuf));
  85.  
  86.       while (zlen--)
  87.     if (substdio_put(&ssout,"",1) == -1)
  88.       strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  89.  
  90.       if (substdio_flush(&ssout) == -1)
  91.     strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  92.       if (fsync(fdout) == -1)
  93.     strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  94.       close(fdout);
  95.       break;
  96.  
  97.     case 'p':
  98. #ifndef __amigaos__
  99.       if (fifo_make(target.s,0700) == -1)
  100.         if (errno != error_exist)
  101.       strerr_die4sys(111,FATAL,"unable to mkfifo ",target.s,": ");
  102.       break;
  103. #endif
  104.  
  105.     case 'd':
  106.       if (mkdir(target.s,0700) == -1)
  107.         if (errno != error_exist)
  108.       strerr_die4sys(111,FATAL,"unable to mkdir ",target.s,": ");
  109.       break;
  110.  
  111.     case 'c':
  112.       fdin = open_read(name);
  113.       if (fdin == -1)
  114.     strerr_die4sys(111,FATAL,"unable to read ",name,": ");
  115.       substdio_fdbuf(&ssin,read,fdin,inbuf,sizeof(inbuf));
  116.  
  117.       fdout = open_trunc(target.s);
  118.       if (fdout == -1)
  119.     strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  120.       substdio_fdbuf(&ssout,write,fdout,outbuf,sizeof(outbuf));
  121.  
  122.       switch(substdio_copy(&ssout,&ssin)) {
  123.     case -2:
  124.       strerr_die4sys(111,FATAL,"unable to read ",name,": ");
  125.     case -3:
  126.       strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  127.       }
  128.  
  129.       close(fdin);
  130.       if (substdio_flush(&ssout) == -1)
  131.     strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  132.       if (fsync(fdout) == -1)
  133.     strerr_die4sys(111,FATAL,"unable to write ",target.s,": ");
  134.       close(fdout);
  135.       break;
  136.  
  137.     default:
  138.       return;
  139.   }
  140.  
  141.   if (chown(target.s,uid,gid) == -1)
  142.     strerr_die4sys(111,FATAL,"unable to chown ",target.s,": ");
  143.   if (chmod(target.s,mode) == -1)
  144.     strerr_die4sys(111,FATAL,"unable to chmod ",target.s,": ");
  145. }
  146.  
  147. char buf[256];
  148. substdio in = SUBSTDIO_FDBUF(read,0,buf,sizeof(buf));
  149. stralloc line = {0};
  150.  
  151. void main(argc,argv)
  152. int argc;
  153. char **argv;
  154. {
  155.   int match;
  156.  
  157.   umask(077);
  158.  
  159.   to = argv[1];
  160.   if (!to) strerr_die2x(100,FATAL,"install: usage: install dir");
  161.  
  162.   for (;;) {
  163.     if (getln(&in,&line,&match,'\n') == -1)
  164.       strerr_die2sys(111,FATAL,"unable to read input: ");
  165.     doit(&line);
  166.     if (!match)
  167.       _exit(0);
  168.   }
  169. }
  170.